const configService = require('../services/configService');

class ConfigController {
    /**
     * 设置运行时authToken
     * POST /api/config/auth-token
     */
    async setAuthToken(ctx) {
        try {
            const { authToken } = ctx.request.body;

            if (!authToken) {
                ctx.status = 400;
                ctx.body = {
                    success: false,
                    message: '请提供authToken配置'
                };
                return;
            }

            const result = configService.setRuntimeAuthToken(authToken);

            if (result.success) {
                ctx.body = {
                    success: true,
                    message: result.message,
                    data: {
                        tokenInfo: {
                            name: authToken.name,
                            userId: authToken.userId,
                            phone: authToken.phone,
                            locale: authToken.locale,
                            expiresDate: new Date(authToken.expires).toLocaleString(),
                            isExpired: Date.now() > authToken.expires
                        }
                    }
                };
            } else {
                ctx.status = 400;
                ctx.body = {
                    success: false,
                    message: result.message
                };
            }

        } catch (error) {
            console.error('设置authToken失败:', error);
            ctx.status = 500;
            ctx.body = {
                success: false,
                message: '服务器内部错误',
                error: error.message
            };
        }
    }

    /**
     * 获取当前配置状态
     * GET /api/config/status
     */
    async getConfigStatus(ctx) {
        try {
            const status = configService.getConfigStatus();

            ctx.body = {
                success: true,
                message: '配置状态获取成功',
                data: status
            };

        } catch (error) {
            console.error('获取配置状态失败:', error);
            ctx.status = 500;
            ctx.body = {
                success: false,
                message: '服务器内部错误',
                error: error.message
            };
        }
    }

    /**
     * 清除运行时authToken
     * DELETE /api/config/auth-token
     */
    async clearAuthToken(ctx) {
        try {
            const result = configService.clearRuntimeAuthToken();

            ctx.body = {
                success: true,
                message: result.message
            };

        } catch (error) {
            console.error('清除authToken失败:', error);
            ctx.status = 500;
            ctx.body = {
                success: false,
                message: '服务器内部错误',
                error: error.message
            };
        }
    }

    /**
     * 测试authToken有效性
     * POST /api/config/auth-token/test
     */
    async testAuthToken(ctx) {
        try {
            const { authToken } = ctx.request.body;

            if (!authToken) {
                ctx.status = 400;
                ctx.body = {
                    success: false,
                    message: '请提供authToken配置'
                };
                return;
            }

            const validation = configService.validateAuthToken(authToken);

            if (validation.valid) {
                ctx.body = {
                    success: true,
                    message: 'authToken验证通过',
                    data: {
                        isValid: true,
                        tokenInfo: {
                            name: authToken.name,
                            userId: authToken.userId,
                            phone: authToken.phone,
                            locale: authToken.locale,
                            expiresDate: new Date(authToken.expires).toLocaleString(),
                            isExpired: Date.now() > authToken.expires
                        }
                    }
                };
            } else {
                ctx.status = 400;
                ctx.body = {
                    success: false,
                    message: `authToken验证失败: ${validation.error}`,
                    data: {
                        isValid: false,
                        error: validation.error
                    }
                };
            }

        } catch (error) {
            console.error('测试authToken失败:', error);
            ctx.status = 500;
            ctx.body = {
                success: false,
                message: '服务器内部错误',
                error: error.message
            };
        }
    }
}

module.exports = new ConfigController();